package com.neusoft.controller;

import com.neusoft.common.Result;
import com.neusoft.pojo.Role;
import com.neusoft.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/*
* 1,什么是rest风格
* 旧的写法 ： RequestMapping("/role/roleById/id=1",method = RequestMethod.GET )
* rest写法： 一个类中重复的内容提出来，比如 @RequestMapping(value = "/role") ,
*            通过4个动作（get,post,put,delete）的mapping 来判断是增删查改
*2,@RestController 注解包含了两个注解，分别为ResponseBody、Controller
*
*
* */
@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /*根据ID查询角色*/
    @GetMapping("/{id}")
    public Result<?> roleById(@PathVariable String id) {
        List<Role> ls = roleService.selectRoleById(id);
        return Result.success(ls);
    }

    /*查询所有角色*/
    @GetMapping
    public Result<?> roleAll(@RequestBody Role role) {
        List<Role> ls = roleService.selectRoleList(role);
        return Result.success(ls);
    }

    /*新增角色*/
    @PostMapping
    public Result<?> roleAdd(@RequestBody Role role) {
        int s = roleService.insertRole(role);
        if (1 == s) {
            return Result.success();
        } else {
            return Result.error("500","新增角色失败");
        }


    }

    /*根据ID更新角色的数据*/
    @PutMapping
    public Result<?> roleUpdate(@RequestBody Role role) {
        int s = roleService.updateRoleById(role);
        if (1 == s) {
            return Result.success();
        } else {
            return Result.error("500","新增角色失败");
        }
    }

    /*删除角色*/
    @DeleteMapping("/{id}")
    public Result<?> roleDelete(@PathVariable String id){
        int s = roleService.deleteRoleById(id);
        if (1 == s) {
            return Result.success();
        } else {
            return Result.error("500","新增角色失败");
        }
    }



}
